﻿using UnityEngine;
using System.Collections;

enum PartType
{
	Body,
	LegLeft,
	LegRight,
	HandLeft,
	HandRight,
	Head,
}

public class HumanProperty : MonoBehaviour 
{
	/*
	身体：长， 宽
	头部：半径
	腿：长度
	手：长度
	肤色：颜色值
	*/


	public int BodyMaxWidth;
	public int BodyMinWidth;
	public int BodyMaxHeight;
	public int BodyMinHeight;
	public int LegMaxLength;
	public int LegMinLength;
	public int HeadMinWidth;
	public int HeadMaxWidth;
	public int HeadMinHeight;
	public int HeadMaxHeight;
	public int HandMinLength;
	public int HandMaxLength;


	public int BodyWidth;
	public int BodyHeight;
	public int HeadWidth;
	public int HeadHeight;
	public int LegLength;
	public int HandLength;

	public Color SkinColor;
	public Texture2D SkinGradient;
	public float SkinColorBias;

	public UISprite BodySprite;
	public UISprite HeadSprite;
	public UISprite LegLeftSprite;
	public UISprite LegRightSprite;
	public UISprite HandLeftSprite;
	public UISprite HandRightSprite;

	public int TotalHeight;
	public float BodyPosY;

	private UIWidget HumanContainer;

	void Awake()
	{
		BodySprite = this.transform.Find("Body").GetComponent<UISprite>();
		HeadSprite = this.transform.Find("Head").GetComponent<UISprite>();
		LegLeftSprite = this.transform.Find("LeftLeg").GetComponent<UISprite>();
		LegRightSprite  = this.transform.Find("RightLeg").GetComponent<UISprite>();
		HandLeftSprite = this.transform.Find("LeftHand").GetComponent<UISprite>();
		HandRightSprite = this.transform.Find("RightHand").GetComponent<UISprite>();
		HumanContainer = this.GetComponent<UIWidget> ();

		SkinColorBias = 1.0f;
		SkinColor = SkinGradient.GetPixelBilinear (SkinColorBias, 0.0f);

	}
	
	void Start () 
	{
		SetTotalHeight ();

		BodyPosY = BodySprite.transform.position.y;

		this.gameObject.SetActive (false);

		Invoke ("SetActiveTrue", 0.1f);
	}

	void SetActiveTrue()
	{
		this.gameObject.SetActive (true);
	}

	void Update () 
	{
		{
			//UpdateShow();
		}
	}

	public int GetLegalValue(int Value, int MinValue, int MaxValue)
	{
		if (Value > MaxValue) 
		{
			return MaxValue;
		} 
		else if(Value < MinValue) 
		{
			return MinValue;
				
		} else 
		{
			return Value;
		}
	}

	public void UpdateShow()
	{
		BodySprite.width = GetLegalValue(BodyWidth, BodyMinWidth, BodyMaxWidth);
		BodySprite.height = GetLegalValue(BodyHeight, BodyMinHeight, BodyMaxHeight);
		
		HeadSprite.height = GetLegalValue(HeadHeight, HeadMinHeight, HeadMaxHeight);
		HeadSprite.width = GetLegalValue(HeadWidth, HeadMinWidth, HeadMaxWidth);
		
		LegLeftSprite.height = GetLegalValue(LegLength, LegMinLength, LegMaxLength);
		LegRightSprite.height = GetLegalValue(LegLength, LegMinLength, LegMaxLength);
		
		HandLeftSprite.height = GetLegalValue(HandLength, HandMinLength, HandMaxLength);
		HandRightSprite.height = GetLegalValue(HandLength, HandMinLength, HandMaxLength);
		
		HeadSprite.color = SkinColor;
		HandLeftSprite.color = SkinColor;
		HandRightSprite.color = SkinColor;

		LegLeftSprite.color = SkinColor;
		LegRightSprite.color = SkinColor;

		SetTotalHeight ();
		UpdateHumanCenterPos ();
	}

	public void RandomProperty()
	{
		BodyWidth = Random.Range (BodyMinWidth, BodyMaxWidth);
		BodyHeight = Random.Range (BodyMinHeight, BodyMaxHeight);

		HeadHeight = Random.Range (HeadMinHeight, HeadMaxHeight);
		HeadWidth = Random.Range (HeadMinWidth, HeadMaxWidth);

		LegLength = Random.Range (LegMinLength, LegMaxLength);

		HandLength = Random.Range (HandMinLength, HandMaxLength);

		//SkinColorBias = Random.Range (0.0f, 1.0f);
		//SkinColor = SkinGradient.GetPixelBilinear (SkinColorBias, 0.0f);
	}

	public void Combine(HumanProperty other)
	{
		BodyWidth = (BodyWidth + other.BodyWidth) / 2;
		BodyHeight = (BodyHeight + other.BodyHeight) / 2;
		HeadWidth = (HeadWidth + other.HeadWidth) / 2;
		HeadHeight = (HeadHeight + other.HeadHeight) / 2;
		HandLength =  (HandLength + other.HandLength) / 2;
		LegLength = (LegLength + other.LegLength) / 2;

		SkinColorBias = (SkinColorBias + other.SkinColorBias) / 2;
		SkinColor = SkinGradient.GetPixelBilinear (SkinColorBias, 0.0f);
	}

	private void SetTotalHeight()
	{
		TotalHeight = BodySprite.height + HeadSprite.height + 3 + LegLeftSprite.height;

		HumanContainer.height = TotalHeight;
	}

	private void UpdateHumanCenterPos()
	{
		//BodySprite.gameObject.transform.Translate (new Vector3 (0, 30, 0));
	}


}
